package org.linlinjava.litemall.admin.job;


import cn.hutool.core.util.StrUtil;
import lombok.RequiredArgsConstructor;
import org.linlinjava.litemall.core.config.DynamicTimeZoneConfiguration;
import org.linlinjava.litemall.core.util.ResponseUtil;
import org.linlinjava.litemall.db.beans.Constants;
import org.linlinjava.litemall.db.dao.LitemallShopMapper;
import org.linlinjava.litemall.db.domain.LitemallShop;
import org.linlinjava.litemall.db.dto.AdminShopSpecialDateDTO;
import org.linlinjava.litemall.db.service.LitemallShopService;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;


import java.text.SimpleDateFormat;
import java.time.*;
import java.time.format.DateTimeFormatter;
import java.util.Arrays;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.concurrent.CompletableFuture;

@Component
@RequiredArgsConstructor
public class AdminShopRestJob {
    private final LitemallShopService litemallShopService;
    @Value("${sys.difference}")
    private Long difference;
    private DynamicTimeZoneConfiguration dynamicTimeZoneConfiguration = new DynamicTimeZoneConfiguration();
    @Scheduled(cron = "0 */1 * * * ?")
    public void shopRestCheckClose() {
        shopCheckWeeksTime();
    }

    private void shopCheckWeeksTime() {
        CompletableFuture.runAsync(() -> {
            //所有门店
            List<LitemallShop> litemallShops = litemallShopService.all();
            litemallShops.stream().forEach(shop -> {

                //动态设置门店时区 按照门店所在地区时间判断是否在营业
                dynamicTimeZoneConfiguration.setDynamicTimeZone(shop.getTimezone());

                // 假设门店的营业时间已经存在于 shop.getWeeksTime() 集合中
                List<LocalTime[]> weeksTime = shop.getWeeksTime();
                if (weeksTime == null) {
                    shop.setStatus((short) 2);
                    litemallShopService.updateById(shop);
                    return;
                }
                // 获取当前日期和时间
                LocalDate currentDate = LocalDate.now();
                LocalTime currentTime = LocalTime.now();
                if(StrUtil.isNotEmpty(shop.getTimezone())){
                    // 设置选择的时区
                    ZoneId zoneId = ZoneId.of(shop.getTimezone());
                    // 获取当前日期
                    currentDate = LocalDate.now(zoneId);
                    // 获取当前时间
                    currentTime = LocalTime.now(zoneId);
                }

                // 获取当前是星期几
                DayOfWeek currentDayOfWeek = currentDate.getDayOfWeek();

                // 获取对应的门店营业时间
                LocalTime[] businessHours = weeksTime.get(currentDayOfWeek.getValue() - 1);

                // 判断当前时间是否处于门店的开业时间范围内
                boolean isOpen = currentTime.isAfter(businessHours[0]) && currentTime.isBefore(businessHours[1]);

                if (isOpen) {
                    System.out.println("门店正在营业。");
                    shop.setStatus((short) 1);
                } else {
                    System.out.println("门店未营业。");
                    shop.setStatus((short) 2);
                }
                List<AdminShopSpecialDateDTO> specialDateList = shop.getSpecial();
                LocalDate today = LocalDate.now();
                // 遍历集合中的每个AdminShopSpecialDateDTO对象
                for (AdminShopSpecialDateDTO dto : specialDateList) {
                    LocalDate specialDate = dto.getSpecialDate();
                    LocalTime[] specialTime = dto.getSpecialTime();

                    // 在这里进行特定逻辑处理，判断门店是否营业
                    // 判断specialDate是否是今天
                    if (!specialDate.isEqual(today)) {
                        continue;
                    }
                    // 判断当前时间是否在营业时间范围内
                    isOpen = currentTime.isAfter(specialTime[0]) && currentTime.isBefore(specialTime[1]);
                    shop.setStatus(isOpen ? (short) 1 : (short) 2);
                }

                //shop.setStatus(isOpen?(short) 1:(short) 2);
                litemallShopService.updateById(shop);
            });
        });
    }


}
